layui.use('form', function () {
    var form = layui.form;
    //发送短信
    $('.countTime').one('click', function () {
        var _this = $(this)
        phoneSms(_this)
    })

    form.on('submit(resSend)', function (data) {
        $.post('/register', data.field, function (res) {
            if (res.code == 20009) {
                layer.msg("该手机号已经注册", {time: 3000})
            } else if (res.code == 10001) {
                    $.post('/login',{'phone':$('#phone').val(),'password':$('#password').val()},function (data) {
                        if(data.code==10004){
                            window.location.href='/toMain'
                        }else{
                            layer.msg(data.message, {time: 3000})
                        }
                    })
            } else if (res.code == 20006) {
                layer.msg("请输入正确的短信验证码!", {time: 3000})
            }
        })
        return false;
    });

    form.verify({
        password: [
            /((?=.*\d)(?=.*\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))^.{8,16}$/, '请输入8-16位且数字、字母、符号至少包含两种'

        ]
    })
    //发送短信验证码
    function phoneSms(_this) {
        $.post('/checkPhone', {'phone': $('#phone').val()}, function (data) {
            if (data.code == 10002) {
                _this.innerHTML = '60秒后重新发送'
                countDown(_this)
                $.post('/sendSms', {'phone': $('#phone').val()}, function (data) {
                    console.log(data)
                    if (data.code != 10000) {
                        layer.msg(data.message, {time: 3000})
                    }
                })
            } else {
                layer.msg(data.message, {time: 3000})
                _this.one('click', function () {
                    phoneSms(_this)
                })
            }
        })
    }
});


//60秒倒计时s
function countDown(dom) {
    var timer, k = 60,
        i = 0
    timer = setInterval(function () {
        i++;
        dom.html((k - i) + '秒后重新发送')
        if ((k - i) == 0) {
            dom.html('获取验证码')
            dom.one('click', function () {
                phoneSms(dom)
            })
            clearInterval(timer)
        }
    }, 1000)
}